from typing import *
from leetcode.editor.common.node import *


# leetcode submit region begin(Prohibit modification and deletion)
class Solution:
    def maximumSubarraySum(self, nums: List[int], k: int) -> int:
        window = {} # 记录当前窗口中有哪些元素
        ans = win_sum = 0
        for right,item in enumerate(nums):
            window[item] = window.get(item,0)+1
            win_sum+=item
            left = right -k+1
            if left >=0:
                if len(window) == k: # 麻了
                    ans = max(ans,win_sum)
                l = nums[left]
                win_sum -= l
                window[l]-=1
                # 不加这个超时了
                if window[l] ==0:
                    del window[l]
        return ans
# leetcode submit region end(Prohibit modification and deletion)


if __name__ == '__main__':
    solution = Solution()
    # your test code here